package com.jiuhao.client;


import cn.hutool.extra.spring.SpringUtil;
import com.jiuhao.api.BaiduApiService;
import com.jiuhao.conf.JiuhaoConfig;
import lombok.RequiredArgsConstructor;
import okhttp3.*;
import okhttp3.logging.HttpLoggingInterceptor;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

@Configuration
@RequiredArgsConstructor
public class RetrofitClient {


    private final JiuhaoConfig jiuhaoConfig;

    @Bean(name = "okHttpClientBaidu")
    public OkHttpClient okHttpClient() {
        // 创建日志拦截器
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY); // 打印完整请求体和响应体
        return new OkHttpClient.Builder()
                .connectTimeout(30, TimeUnit.SECONDS)
                .readTimeout(30, TimeUnit.SECONDS)
                .writeTimeout(30, TimeUnit.SECONDS)
                .addInterceptor(new HeaderInterceptor())
                .addInterceptor(loggingInterceptor)
                .build();
    }

    @Bean(name = "retrofitBaidu")
    public Retrofit retrofit(@Qualifier("okHttpClientBaidu") OkHttpClient okHttpClient) {
        return new Retrofit.Builder()
                .baseUrl(jiuhaoConfig.getUrl())
                .client(okHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Bean
    public BaiduApiService baiduApiService(@Qualifier("retrofitBaidu") Retrofit retrofit) {
        return retrofit.create(BaiduApiService.class);
    }

    private static class HeaderInterceptor implements Interceptor {

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request original = chain.request();

            JiuhaoConfig JiuhaoConfiBean = SpringUtil.getBean(JiuhaoConfig.class);
            // 构建通用请求头
            Request.Builder requestBuilder = original.newBuilder()
                    .header("Accept", "*/*")
                    .header("Accept-Encoding", "gzip, deflate, br")
                    .header("Accept-Language", "zh-CN,zh;q=0.9")
                    .header("Cache-Control", "no-cache")
                    .header("Connection", "keep-alive")
                    .header("Pragma", "no-cache")
                    .header("Sec-Fetch-Dest", "empty")
                    .header("Sec-Fetch-Mode", "cors")
                    .header("Sec-Fetch-Site", "same-origin")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36")
                    .header("sec-ch-ua-mobile", "?0")
                    .header("sec-ch-ua-platform", "Windows")
                    .header("token", "123")
                    .header("tenantId", "34451725346000000");

            // 添加Cookie（实际应用中应从配置或Session获取）
            requestBuilder.header("Cookie", JiuhaoConfiBean.getCookie());

            Request request = requestBuilder.build();
            return chain.proceed(request);
        }
    }
}
